package vendalenger.port;

import java.util.ArrayList;

public class Command {

	public static ArrayList<Command> commandList = new ArrayList<Command>();
	public static ArrayList<Command> hiddenList = new ArrayList<Command>();

	public static void addCommand(Command c, boolean hidden) {
		if (!hidden) {
			commandList.add(c);
		}else{
			hiddenList.add(c);
		}
	}
	
	public static void issue(String said) {
		if (said.startsWith(String.valueOf('^'))) {
			boolean error = true;
			String raw = said.substring(1);
			for (int i = 0; i < commandList.size(); i++) {
				if (commandList.get(i).key.equalsIgnoreCase(raw.split(" ")[0])) {
					error = false;
					try {
						commandList.get(i).action(
								raw.substring(raw.split(" ")[0].length() + 1)
										.split("  "));
					} catch (StringIndexOutOfBoundsException e) {
						commandList.get(i).action(new String[] { "null" });
					}
				}
			}
			for (int i = 0; i < hiddenList.size(); i++) {
				if (hiddenList.get(i).key.equalsIgnoreCase(raw.split("  ")[0])) {
					error = false;
					try {
						hiddenList.get(i).action(
								raw.substring(raw.split("  ")[0].length() + 1)
										.split("  "));
					} catch (StringIndexOutOfBoundsException e) {
						hiddenList.get(i).action(new String[] { "null" });
					}
				}
			}
			if (error) {
				System.out.println("Command: " + raw.split("\\(")[0]
						+ " not found.");
				System.out.println("Try ^help for some options.");
			}
		}
	}

	public String key, help, usage;

	/**
	 * Create a command that could be accessed through the console, player chat,
	 * and so on.
	 * 
	 * @param keyword
	 *            the name of the command example: keyword()
	 * @param quickhelp
	 *            the quick statement shown when help is called.
	 * @param use
	 *            the message given, usually an example, of how to use this
	 *            method. Automatically begins with "Usage for command: keyword"
	 */
	public Command(String keyword, String quickhelp, String use) {
		key = keyword;
		help = quickhelp;
		usage = use;
	}

	/**
	 * Called when the command is issued.
	 * 
	 * @param args
	 *            arguments included in the issue example: keyword(arg1, arg2)
	 */
	public String action(String[] args) {
		return null;
	}

}